<?php 
	session_start();
	error_reporting(0);
	include 'include/sql.php';
?>
<!DOCTYPE HTML>
<html>
	<head>
		<title>
			Quản lý người chơi
		</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0" />
		<meta http-equiv="x-ua-compatible" content="IE=edge">
		<link type="text/css" href="CSS/jquery-ui-1.10.3.custom.min.css" rel="stylesheet"/>
		<link type="text/css" href="CSS/ui.jqgrid.css" rel="stylesheet"/>
		<link type="text/css" href="CSS/jquery.multiselect.css" rel="stylesheet"/>
		<link type="text/css" href="CSS/stylesheet.css" rel="stylesheet" media="screen"/>
		<link type="text/css" href="CSS/new-css.css" rel="stylesheet"/>
		<script type="text/javascript" src="Javascript/jquery-2.0.3.min.js"></script>
		<script type="text/javascript" src="Javascript/jquery-ui-1.10.3.custom.min.js"></script>
		<script type="text/javascript" src="Javascript/jquery.jqGrid.min.js"></script>
		<script type="text/javascript" src="Javascript/grid.locale-en.js"></script>
		<script type="text/javascript" src="Javascript/menu.js"></script>
		<script type="text/javascript" src="Javascript/jquery.multiselect.min.js"></script>
		<script type="text/javascript">
			// For edit and delete
			function delById(id){
				var cfm = confirm("Bạn có thực sự muốn xóa người chơi này không?");
				if(cfm == false) return;
				$.ajax({
					url: 'delete-member.php?id='+id,
					type: 'GET',
					dataType: 'text',
					success: function(res){
						if(res != null && res.indexOf("success") > -1 ){
							$("#list2").jqGrid("delRowData",id).trigger("reloadGrid");
						}
						
					}
				});
			}
			function viewById(id){
				var ret = $("#list2").jqGrid("getRowData",id);
				$("#_k_infor_detail_id").text(ret.id);
				$("#_k_infor_detail_fullname").text(ret.name);
				$("#_k_infor_detail_email").text(ret.email);
				$("#_k_infor_detail_phoneno").text(ret.phoneno);
				$("#_k_infor_detail_soluotchoiconlai").text(ret.so_luot_choi_con_lai);
				$("#_k_infor_detail_soluotchoirefconlai").text(ret.so_luot_choi_ref_con_lai);
				$("#_k_infor_detail_soluotdachoi").text(ret.so_luot_da_choi);
				$("#_k_infor_detail_songuoigioithieu").text(ret.so_nguoi_gioi_thieu);
				$("#_k_infor_detail_tongdiem").text(ret.total_point);
				$("#_k_infor_detail_islucky").text(ret.is_lucky == 0?"Chưa":"Rồi");
				$("#_k_infor_detail_activate").text(ret.is_activated == 1?"Rồi":"Chưa");
				
				$("#_k_view_infor").dialog({
					height: 350,
			      	width: 450,
			      	modal: true,
			      	buttons: {
						"Đóng": function(){
									$(this).dialog("close");
						}
					}
				});
			}
			$(document).ready(function(){
				
				
				
				//handle login event
				$("#login").click(
					function(e){
						e.preventDefault();
						// Logout
						if($("#login>span").text().search("out") > -1){
							$.ajax({
								url: "log-out.php",
								type: "POST",
								success: function(res){
									window.setTimeout('location.reload()', 1);
								}
							});	
							return;
						}
						// Login
						var adminname = $("#_k_admin_name").val();
						var adminpsw = $("#_k_admin_psw").val();
						if(adminname === "" || adminpsw === ""){
							alert("Please enter both name and password to login.");
							return;
						}
						$.ajax({
							url: "login-action.php",
							dataType: "json",
							type: "POST",
							data: {adminname: adminname,adminpsw: adminpsw},
							success: function(response){
								window.setTimeout('location.reload()',1);
							},
							error: function(data){								
								alert(data);
							}
						});
						
					}		
				);
				//change password of admin
				$("#admin_changepsw").click(
					function(e){
						e.preventDefault();						
						$("#_k_change_password").dialog({							
					      	height: 200,
					      	width: 350,
					      	modal: true,
					        buttons: {
					          "Submit": function() {
						        var oldpsw = $("#_k_old_password").val();
						        var newpsw = $("#_k_new_password").val();
						        var confirmpsw = $("#_k_confirm_password").val();
						        if(oldpsw == '' || newpsw == '' || confirmpsw == ''){
										alert("Please fill in all fields.");
										if(oldpsw == '') {
											$("#_k_old_password").focus();
											return;
										}
										if(newpsw == '') {
											$("#_k_new_password").focus();
											return;
										}
										if(confirmpsw == '') {
											$("#_k_confirm_password").focus();
											return;
										}
							    }
							    if(newpsw !== confirmpsw){
							    	alert("Passwords not match.");
							    	$("#_k_new_password").focus();
							    	return;
								}
								$.post("change-psw.php",{oldpsw:oldpsw,newpsw:newpsw},
										function(response){		
										if(response.indexOf("success")>-1)	{				
// 										if(res.result == 'successful'){
											alert("Password changed successfully!");
										}
										if(response.indexOf("fail")>-1){
											alert("Password changed fail!");
										}
										$( "#_k_change_password" ).dialog( "close" );
									});
								
					          },
					          Cancel: function() {
					            $( this ).dialog( "close" );
					          }
					        }
						});
					}
				);
			});
		</script>
	</head>
	<body>
		<div id="container">
			<div id="header">
				<div class="topHeader">
					<div class="top-left-Header">
						<img alt="LOGO" src="Images/logo.png" width="180" height="29" class="logo"/>
					</div>
					<div class="top-right-Header">
						<ul><?php if(!isset($_SESSION['admin'])){?>
							<li id="_k_welcome">
								<span style="font-weight: bold; color: #e9e9e9"></span>
							</li>
							<li class="_k_will_be_hidden">
								<span style="font-weight: bold; color: #e9e9e9">Tài khoản: </span>
								<input type="text" name="userName" placeholder="admin name" id="_k_admin_name"/></li>
							<li class="_k_will_be_hidden">
								<span style="font-weight: bold; color: #e9e9e9">Mật khẩu: </span>
								<input type="password" name="password" placeholder="password" id="_k_admin_psw"/>
							</li>
							<?php }
								else{
							?>
							<script type="text/javascript">
							$(document).ready(
									function(){
											$.ajax({
												url: "get-data.php",
												type: "POST",
												dataType: "json",
												error: function(e){
													alert(e);
												},
												success: function(response){
													var mydata = response;
													GlobalScript.gridName = "list2"; // set with when resize window
													jQuery("#list2").jqGrid({ 
														data: mydata,
														datatype: "local", 
														height: 300,
														autowidth: true,
														colNames: ['ID','Tên','Email',"Điện thoại","Turns left","Ref turns left","Da choi","Số người đã giới thiệu","Ngày tham gia", 'Tổng điểm',"Islucky",
																	"Newturn","Is activated",'Action'], 
														colModel: [ 
																{name:'id',index:'id', search:false},
																{name:'name',index:'name',searchoptions:{sopt:['eq','cn']}}, 
																{name:'email',index:'email',searchoptions:{sopt:['eq','cn']}}, 
																{name:'phoneno',index:'phoneno', align:"right", hidden:true}, 
																{name:'so_luot_choi_con_lai',index:'so_luot_choi_con_lai',hidden:true}, 
																{name:'so_luot_choi_ref_con_lai',index:'so_luot_choi_ref_con_lai',align:"right", hidden:true},
																{name:'so_luot_da_choi',index:'so_luot_da_choi',align:"right", hidden:true}, 
																{name:'so_nguoi_gioi_thieu',index:'so_nguoi_gioi_thieu',align:"right"},
																{name:'ngay_tham_gia',index:'ngay_tham_gia',align:"right", search:false},
																{name:'total_point',index:'total_point',align:"right", search:false}, 
																{name:'is_lucky',index:'is_lucky',align:"right",  hidden:true},
																{name:'new_turns_received',index:'new_turns_received',align:"right", hidden:true},
																{name:'is_activated',index:'is_activated',align:"right", hidden:true},
																{name:'action',index:'action',align:"center", search:false, sortable:false},
																],
													  rowNum:10, 
													  rowList:[10,20,30], 
													  pager: '#pager2', 
													  viewrecords: false,
													  sortorder: "desc",
													  cmTemplate: {title: false},

													  gridComplete: function() {
															var ids = jQuery("#list2").jqGrid('getDataIDs');
															for (var i = 0; i < ids.length; i++) {
																var cl = ids[i];
																sp = "<span> | </span>";
																view = "<a href='#' onClick=\" viewById('"+cl+"');\" >View</a>";
																del  = "<a href='#' onClick=\" delById('"+cl+"');\" >Delete</a>";
																jQuery("#list2").jqGrid('setRowData', cl, {action: view + sp + del});
															}
													  }
													});


													  
													  jQuery("#list2").jqGrid('navGrid', "#pager2", 								{
															edit:false,
															add:false,
															del:false, 
															search:true,
															refresh: true});
													  jQuery("#list2").jqGrid('clearGridData');
													  jQuery("#list2").jqGrid('setGridParam', {data: mydata});
													  jQuery("#list2").trigger('reloadGrid');
												}
											});
									}
							);
							
							</script>
							<li id="_k_welcome">
								<span style="font-weight: bold; color: #e9e9e9">Welcome back, admin! | </span>
								<span id="admin_changepsw">Change Password</span>
							</li>
							<?php }?>
							<li>
								<button id="login">
									<span class="button">
										<?php if(!isset($_SESSION['admin'])){
											echo "Log in";
											
										}else echo ("Log out");?>
									</span>
								</button>
							</li>
						</ul>
					</div>
				</div>
				<div class="botton-header"></div>
			</div>
			<div id="content">
				<div class="left-content">
			</div>
				<div class="right-content">
					<div class="title">
						<div class="left-title">
							Quản lý người chơi
						</div>
					
					</div>
					<div class="wrapper">
						<div class="main-wrapper">
							<table id="list2" class="bordered">
								
							</table> 
							<div id="pager2"></div>
							
							<div class="container-button">
							
							</div>
						</div>
					</div>
				</div>
				<div id="footer">
					<div class="content-footer">
						Copyright &copy; AnhND
					</div>
					
				</div>
				<div style="clear: both;"></div>
		</div>
		<!-- Div for change password -->
		<div id="_k_change_password" title="Change password">
			<table>
				<tr>
					<td>Old password:</td>
					<td><input type="password" id="_k_old_password"></td>
				</tr>
				<tr>
					<td>New password:</td>
					<td><input type="password" id="_k_new_password"></td>
				</tr>
				<tr>
					<td>Confirm password:</td>
					<td><input type="password" id="_k_confirm_password"></td>
				</tr>
				
			</table>
		</div>
		<!-- Div for view infor -->
		<div id="_k_view_infor" title="Chi tiết người chơi">
			<table class="zebra">
				<tr>
					<td>ID:</td>
					<td><span id="_k_infor_detail_id"></span></td>
				</tr>
				<tr>
					<td>Tên người chơi:</td>
					<td><span id="_k_infor_detail_fullname"></span></td>
				</tr>
				<tr>
					<td>Email:</td>
					<td><span id="_k_infor_detail_email"></span></td>
				</tr>
				<tr>
					<td>Số điện thoại:</td>
					<td><span id="_k_infor_detail_phoneno"></span></td>
				</tr>
				<tr>
					<td>Số lượt chơi hàng ngày còn lại:</td>
					<td><span id="_k_infor_detail_soluotchoiconlai"></span></td>
				</tr>
				<tr>
					<td>Số lượt chơi kiếm được còn lại:</td>
					<td><span id="_k_infor_detail_soluotchoirefconlai"></span></td>
				</tr>
				<tr>
					<td>Số lượt đã chơi:</td>
					<td><span id="_k_infor_detail_soluotdachoi"></span></td>
				</tr>
				<tr>
					<td>Số người đã giới thiệu:</td>
					<td><span id="_k_infor_detail_songuoigioithieu"></span></td>
				</tr>
				<tr>
					<td>Tổng điểm:</td>
					<td><span id="_k_infor_detail_tongdiem"></span></td>
				</tr>
				<tr>
					<td>Từng nhận quà may mắn chưa?</td>
					<td><span id="_k_infor_detail_islucky"></span></td>
				</tr>
				<tr>
					<td>Activate chưa?</td>
					<td><span id="_k_infor_detail_activate"></span></td>
				</tr>
			</table>
		</div>
	</body>
</html>